package models

import "time"

const UserInfoTableName string = "user_info"

type UserInfoStruct struct {
	UserID   string    `xorm:"user_id"`
	UserName string    `xorm:"user_name"`
	Age      int       `xorm:"age"`
	Created  time.Time `xorm:"created"`
	Updated  time.Time `xorm:"updated"`
}

func (u *UserInfoStruct) TableName() string {
	return UserInfoTableName
}

func (u *UserInfoStruct) InsertUserInfo() error {
	if _, err := DB.M1.InsertOne(u); err != nil {
		return err
	}
	return nil
}

// 从数据库中获取前100条用户数据
func GetTop100UserInfoRecords() ([]UserInfoStruct, error) {
	records := make([]UserInfoStruct, 0)
	if err := DB.M1.Desc("created").Limit(100).Find(&records); err != nil {
		return nil, err
	}
	return records, nil
}
